package com.kelvin.paopao.module.market.market.takeorder;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.GoodDetailModel;
import com.kelvin.paopao.api.responseBean.LocationPickModel;
import com.kelvin.paopao.module.market.market.order.OrderActivity;
import com.kelvin.paopao.utils.DialogMaker;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/25.
 */

public class TakeOrderPresenter implements ITakeOrderPresenter {
    private TakeOrderActivity mView;

    public TakeOrderPresenter(TakeOrderActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(String id) {
        RetrofitService.getLocationPick().subscribe(new Consumer<CommonListResponse<LocationPickModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<LocationPickModel> locationPickModelCommonListResponse) throws Exception {
                if (locationPickModelCommonListResponse != null && locationPickModelCommonListResponse.getResult().size() != 0) {
                    //刷新界面
                    mView.showLocation(locationPickModelCommonListResponse.getResult().get(0));
                }
            }
        });

        RetrofitService.getGoodDetail(id).subscribe(new Consumer<CommonListResponse<GoodDetailModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<GoodDetailModel> commonListResponse) throws Exception {
                if (commonListResponse != null && commonListResponse.getResult().size() != 0) {
                    mView.showGoodDetail(commonListResponse.getResult().get(0));
                }
            }
        });
    }

    @Override
    public void takeOrder(String sId, String aId,
                          String oNum, String oPayMode, String oRemarks) {
        RetrofitService.takeorder(sId, aId, oNum, oPayMode, oRemarks).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView, "正在生成订单~~~");
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse>() {
                    @Override
                    public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                        if (commonListResponse.getReason().equals("成功")) {
                            ToastUtil.showToast(mView, "下单成功");
                            mView.finish();
                            OrderActivity.launch(mView);
                        } else {
                            ToastUtil.showToast(mView, commonListResponse.getReason());
                        }

                        DialogMaker.diamiss();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        DialogMaker.diamiss();
                    }
                });
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }
}
